package com.cassio.stores.view;

import java.util.List;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ZoomControls;

import com.cassio.stores.R;
import com.cassio.stores.util.AppConstants;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class StoresMapActivity extends MapActivity {

	private MapView mapView;
	private MapController mapController;
	private ZoomControls zoomControl;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stores_map);
        mapView = (MapView) findViewById(R.id.mapView);
        mapController = mapView.getController();
        
        zoomControl = (ZoomControls) findViewById(R.id.zoomControl);
        zoomControl.setOnZoomInClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				mapController.zoomIn();
			}
		});
        zoomControl.setOnZoomOutClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		mapController.zoomOut();
        	}
        });
        
        //TODO: shared preferences setSatellite
        mapView.setSatellite(false);
        mapController.setZoom(18);
        
        findMyLocation();
        
    }

	private void findMyLocation() {
		List<Overlay> overlays = mapView.getOverlays();
        
        MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
        
        overlays.add(myLocationOverlay);
        
        myLocationOverlay.enableMyLocation();
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_HIGH);
        
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation("gps");
        updateLocation(location);
        locationManager.requestLocationUpdates(provider, 2000, 20, locationListener);
	}

    @Override
    protected boolean isRouteDisplayed() { return false; }
    
    private void updateLocation(Location location) {
    	if(location != null){
    		Double latitude = location.getLatitude()*1E6;
    		Double longitude = location.getLongitude()*1E6;
    		
    		GeoPoint geoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());
    		
    		mapController.animateTo(geoPoint);
    		Log.i(AppConstants.LOG_TAG, "Updating location Lat: " + geoPoint.getLatitudeE6() + " - Lon: "+ geoPoint.getLongitudeE6());
    	}
    	
    }
    
    LocationListener locationListener = new LocationListener() {
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
		public void onProviderEnabled(String provider) {
		}
		public void onProviderDisabled(String provider) {
			updateLocation(null);
		}
		public void onLocationChanged(Location location) {
			updateLocation(location);
		}
	};
}